package com.example.blog.util;

/**
 * @Author: lu
 * @Date: 2023-09-06-11:31
 * @Description:   结果返回类
 */

import lombok.Data;
import java.io.Serializable;


@Data
public class JsonResult<T> implements  Serializable{
    /**
     * 总数
     */
    private static final long serialVersionUID = 1L;
    public static final int SUCCESS = 200;
    public static final int error = 500;

    /**
     * 默认200
     */
    private  Integer code = 200;

    /**
     * 默认成功
     */
    private  String msg = "SUCCESS";

    /**
     * 使用泛型封装
     */
    private T data;

    /**
     * 时间
     */
    private String date;


    public static <T> JsonResult<T> success() {
        return jsonResult(null, SUCCESS, "SUCCESS");
    }

    public static <T> JsonResult<T> success(T data) {
        return jsonResult(data, SUCCESS, "SUCCESS");
    }

    public static <T> JsonResult<T> success1(T data ,int code,String date) {
        return jsonResult1(data, code, "文件上传成功",date);
    }



    public static <T> JsonResult<T> error() {
        return jsonResult(null, error, "Failed");
    }

    public static <T> JsonResult<T> error(String msg) {
        return jsonResult(null, error, msg);
    }

    public static <T> JsonResult<T> error(T data) {
        return jsonResult(data, error, "Failed");
    }

    private static <T> JsonResult<T> jsonResult(T data, Integer code, String msg) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    private static <T> JsonResult<T> jsonResult1(T data, Integer code, String msg,String date) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        result.setDate(date);
        return result;
    }


}
